﻿namespace ch06.binary.interpreter
{
    public static partial class Instr
    {
        // hack!
        private static void Call(ref Vm vm, object args)
        {
            var idx = (uint) args;
            switch (vm.Module.ImportSec[idx].Name)
            {
                case "assert_true":
                    assertEq(vm.OperandStack.popBool(), true);
                    break;
                case "assert_false":
                    assertEq(vm.OperandStack.popBool(), false);
                    break;
                case "assert_eq_i32":
                    assertEq(vm.OperandStack.popU32(), vm.OperandStack.popU32());
                    break;
                case "assert_eq_i64":
                    assertEq(vm.OperandStack.PopU64(), vm.OperandStack.PopU64());
                    break;
                case "assert_eq_f32":
                    assertEq(vm.OperandStack.popF32(), vm.OperandStack.popF32());
                    break;
                case "assert_eq_f64":
                    assertEq(vm.OperandStack.popF64(), vm.OperandStack.popF64());
                    break;
                default:
                    Panic.Run("TODO");
                    break;
            }
        }

        private static void assertEq(object a, object b)
        {
            if (!a.Equals(b))
            {
                Panic.Run("{0} != {1} ", a, b);
            }
        }
    }
}